/**
 * Created with Intellij IDEA.
 * Description:
 * User: 86135
 * Date: 2023-09-29
 * Time: 4:36
 */
import java.util.ArrayList;

public class PalindromicPrimes {

    public static void main(String[] args) {
        ArrayList<Integer> palindromicPrimes = new ArrayList<>();
        int count = 0;
        int num = 2;

        while (count < 20) {
            if (isPalindrome(num) && isPrime(num)) {
                palindromicPrimes.add(num);
                count++;
            }
            num++;
        }

        for (int i = 0; i < palindromicPrimes.size(); i++) {
            System.out.print(palindromicPrimes.get(i) + "\t");
            if ((i + 1) % 10 == 0) {
                System.out.println();
            }
        }
    }

    public static boolean isPalindrome(int num) {
        int reversed = 0;
        int original = num;
        while (num != 0) {
            reversed = reversed * 10 + num % 10;
            num /= 10;
        }
        return original == reversed;
    }

    public static boolean isPrime(int num) {
        if (num <= 1) {
            return false;
        }
        for (int i = 2; i <= Math.sqrt(num); i++) {
            if (num % i == 0) {
                return false;
            }
        }
        return true;
    }
}

